<?php

namespace App\Controller\Home;

use App\Controller\Example\CartExample;
use App\Controller\Example\CartGoodsExample;
use App\Controller\Example\UserExample;
use App\Models\BarsGoodsModel;
use App\Models\CartGoodsModel;
use App\Models\CartModel;
use App\Models\Conn;
use App\Models\UsersModel;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Result\Result;
use Core\Utils\Utils;

class Cart
{
    /**
     * 购物车商品列表
     * entry  token
     * return json
     * */
    public function cartList($data){
        $assemble = Common::assemble(new UserExample(), $data);
        $conn = new Conn();
        $userInside = UsersModel::getUserInside($conn, $assemble->token);
        $cartGoodsExample = new CartGoodsExample();
        $cartGoodsExample->uid = $userInside["id"];
        $cartGoodsInside = CartGoodsModel::getCartGoodsInside($conn, $cartGoodsExample);
        foreach ($cartGoodsInside as $item=>$value){
            $cartGoodsInside[$item]["goodsImg"] = BarsGoodsModel::getGoodsSingle($conn,$value["gid"])["img"];
            $cartGoodsInside[$item]["sku"] = BarsGoodsModel::getGoodsSku($conn, $value["gid"], $value["sku_id"]);
        }
        Result::success($cartGoodsInside);
    }
    /**
     * 加入购物车商品
     * entry  token,gid,number,sku_id
     * return json
     * */
    public function addCart($data){
        $assemble = Common::assemble(new CartGoodsExample(), $data);
        if($assemble->number <=0 || $assemble->number >100){
            Result::error(ErrorEnum::QUANTITYERROR);
        }
        $conn = new Conn();
        $userInside = UsersModel::getUserInside($conn, $assemble->token);
        $goodsSingle = BarsGoodsModel::getGoodsSingle($conn, $assemble->gid);
        $goodsSku = BarsGoodsModel::getGoodsSku($conn, $assemble->gid, $assemble->sku_id);
        if(empty($goodsSingle)){
            Result::error(ErrorEnum::ITEMDOESNOTEXIST);
        }
        if(empty($goodsSku)){
            Result::error(ErrorEnum::PLEASESELECTASPECIFICATION);
        }
        $assemble->uid = $userInside["id"];
        $assemble->name = $goodsSingle["name"];
        $assemble->price = $goodsSku["price"] ? :$goodsSingle["price"];
        $assemble->total = $assemble->price * $assemble->number;
        $assemble->integral = ($goodsSku["integral"] ? :$goodsSingle["integral"]) * $assemble->number;
        $assemble->type = $goodsSingle["type"];
        $conn->shiwuStart();
        if(CartGoodsModel::insertCartGoods($conn, $assemble) != 1){
            $conn->shiwuEnd();
            Result::error(ErrorEnum::FAILEDTOADDTOSHOPPINGCART);
        }
        $ret["id"] = $conn->mysql_insert_id();
        $ret["type"] = $assemble->type;
        $conn->shiwuEnd();
        Result::success($ret);
    }
    /**
     * 编辑购物数量
     * entry  token,id,number
     * return json
     * */
    public function editCartGoodsNumber($data){
        $assemble = Common::assemble(new CartGoodsExample(), $data);
        if($assemble->number <=0 || $assemble->number >100){
            Result::error(ErrorEnum::QUANTITYERROR);
        }
        $conn = new Conn();
        $assemble->uid = UsersModel::getUserInside($conn, $assemble->token)["id"];
        $cartGoodsInsideId = CartGoodsModel::getCartGoodsInsideId($conn, $assemble);
        if(empty($cartGoodsInsideId)){
            Result::error(ErrorEnum::ITEMSTHATDONOTEXISTINTHESHOPPINGCART);
        }
        $goodsSingle = BarsGoodsModel::getGoodsSingle($conn, $cartGoodsInsideId["gid"]);
        $goodsSku = BarsGoodsModel::getGoodsSku($conn, $cartGoodsInsideId["gid"], $cartGoodsInsideId["sku_id"]);
        $assemble->price = $goodsSku["price"] ? :$goodsSingle["price"];
        $assemble->total = $assemble->price * $assemble->number;
        $assemble->integral = ($goodsSku["integral"] ? :$goodsSingle["integral"]) * $assemble->number;
        $conn->shiwuStart();
        if(CartGoodsModel::editCartGoodsInside($conn, $assemble) != 1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::MODIFICATIONFAILED);
        }
        $conn->shiwuEnd();
        Result::success("ok");
    }
    /**
     * 删除购物车商品
     * entry  token,id
     * return json
     * */
    public function delCartGoods($data){
        $assemble = Common::assemble(new CartGoodsExample(), $data);
        $conn = new Conn();
        $assemble->uid = UsersModel::getUserInside($conn, $assemble->token)["id"];
        $cartGoodsTotalInsideIdsCount = CartGoodsModel::getCartGoodsInsideIdsCount($conn, $assemble)["count"];
        if($cartGoodsTotalInsideIdsCount <= 0){
            Result::error(ErrorEnum::ITEMSTHATDONOTEXISTINTHESHOPPINGCART);
        }
        $conn->shiwuStart();
        $assemble->status = -1;
        if(CartGoodsModel::editCartGoodsInside($conn,$assemble) != $cartGoodsTotalInsideIdsCount){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SETTLEMENTFAILED);
        }
        $conn->shiwuEnd();
        Result::success("ok");
    }
    /**
     * 购物车结算(生成订单)
     * entry  token,id,type
     * return json
     * */
    public function cartSettlement($data){
        $assemble = Common::assemble(new CartGoodsExample(), $data);
        if(empty($assemble->type)){
            $assemble->type =1;
        }
        if(empty($assemble->id) || empty($assemble->type)){
            Result::error(ErrorEnum::PLEASESELECTTHEPRODUCTYOUPURCHASED);
        }
        $conn = new Conn();
        $assemble->uid = UsersModel::getUserInside($conn, $assemble->token)["id"];
        $cartGoodsTotal = CartGoodsModel::getCartGoodsTotalInsideIds($conn, $assemble);
        $cartGoodsTotalInsideIdsCount = CartGoodsModel::getCartGoodsTotalInsideIdsCount($conn, $assemble)["count"];
        if($cartGoodsTotalInsideIdsCount <=0 ){
            Result::error(ErrorEnum::THEREISNOQUERYTOSETTLETHEGOODS);
        }
        $cartExample = new CartExample();
        $cartExample->serial = Utils::createOrderNo();
        $cartExample->uid = $assemble->uid;
        $cartExample->price = $cartGoodsTotal["total"];
        $cartExample->pay_price = $cartExample->price;
        $cartExample->integral = 0;
        $cartExample->type = $assemble->type;
        if($cartGoodsTotal["integral"] >0){
            $cartExample->integral = $cartGoodsTotal["integral"];
        }
        $conn->shiwuStart();
        if(CartModel::insertCart($conn,$cartExample) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SETTLEMENTFAILED);
        }
        $assemble->cid = $cartExample->id = $conn->mysql_insert_id();
        $assemble->status = 2;
        if(CartGoodsModel::editCartGoodsInside($conn,$assemble) != $cartGoodsTotalInsideIdsCount){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SETTLEMENTFAILED);
        }
        $conn->shiwuEnd();
        Result::success(CartModel::getCartInsideId(new Conn(),$cartExample));
    }
}